/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package properties.file.translator.components.languages_panel;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import properties.file.translator.language.Language;

/**
 *
 * @author ralph
 */
public class LanguagePanel extends javax.swing.JFrame {

    private int length = Language.values().length;
    private JCheckBox[] checkBoxArray = new JCheckBox[length + 1];

    /**
     * Creates new form LanguagePanel
     */
    public LanguagePanel() {
        initComponents();
        mountContentPanel();
        pack();
    }

    private void mountContentPanel() {
        createCheckBoxes();
        checkBoxArray[length] = new JCheckBox("All Languages");
        checkBoxArray[length].setName("all_languages");
        checkBoxArray[length].addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                toggleAllCheckBoxes(e);
            }
        });
        getContentPane().add(checkBoxArray[length]);
    }

    private void toggleAllCheckBoxes(ItemEvent e) {
        JCheckBox checkBox = (JCheckBox) e.getItemSelectable();
        for (JCheckBox box : checkBoxArray) {
            box.setSelected(checkBox.isSelected());
        }
    }

    private int createCheckBoxes() {
        int count = 0;
        for (Language language : Language.values()) {
            checkBoxArray[count] = new JCheckBox(language.toString());
            checkBoxArray[count].setName(language.getCode());
            getContentPane().add(checkBoxArray[count]);
            ++count;
        }
        return count;
    }

    public void disableCheckBox(String code) {
        for (JCheckBox checkBox : checkBoxArray) {
            if (checkBox.getName().equalsIgnoreCase(code)) {
                checkBox.setSelected(false);
                checkBox.setEnabled(false);
            }
        }
    }

    public void disableAllCheckBox() {
        for (JCheckBox checkBox : checkBoxArray) {
            checkBox.setSelected(false);
            checkBox.setEnabled(false);
        }
    }    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout(8, 5, 18, 18));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LanguagePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LanguagePanel().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
